Passing variables to functions
Passing variables to functions was a quick guide Andrew Gower made in his programming zone around 1998-1999. It read: Note:All underscores were spaces! ---- Passing variables to functions ----------------------------------- *Lets try a quick example *main() *{ *_____int num1=50; *_____int num2=150; *_____int result; *_____result=addnumbers(num1,num2); *_____printf("Result is: %d \n",result); *} *int addnumbers(int a, int b) *{ *_____int c; *_____c=a+b; *_____return c; *} *Ok, the program above demonstrates a completely pointless function called *addnumbers *(being as you could just do it the normal way using a + symbol!) *look at the line *int addnumbers(int a, int b) *You will notice that before the name of the function, is the word int. This *means that *this function is going to return a variable of type int. You will also notice *that inside the brackets *after the function are 2 variable defintions seperated by a comma. This tells *the computer which *variables the function expects to have passed to it, in this case 2 integers. *When the function starts properly, it already has the two variables a and b *available to use as it *likes. These behave as local variables, so any alterations it makes to a and *b, won't affect the copies *in the main function! When the function has finished doing whatever it does *it has the option to return *as answer due to the 'int' we placed before the function name. The function *returns the answer using *the return command followed by the variable it should return. *Lets look at how the main subroutine calls the function addnumbers to make *things really clear *the key line is: result=addnumbers(num1, num2); *the variables to pass to the function are placed inside the brackets, they *must appear in the same order *as the variables we specified in the function. The compiler will copy num1 *into a, and num 2 into b when the *function is called. *As the function returns a variable we have to tell the program where to put *it, this is why we have the *result= bit at the start. It would actually be possible, (but in this case *rather pointless) *to call the function just with: *addnumbers(num1, num2); In which case the answer it returns would just be *thrown away. *You can pass as many variables as you like to a function, they don't all have *to be of the same type, *but they must all be seperated by commas. A function can only return 1 *variable however which is *a bit of a nusisance, but you can get around it by using arrays. Passing arrays to functions -------------------------------- *When you pass an array to a function things behave a bit differently to how *you'd expect. *Unlike when you pass a variable the array isn't copied, so if the function *makes any alterations *to the array that is given to it, it will affect the version in the main *function! This is however *normally very convenient, as it provides a way of returning more than one *answer. After you've been *using C a bit it seems the natural way to do things! :-) *One last example of passing arrays to functions *main() *{ *____int array5={7,12,5,2,4}; *____int var=67; *___//notice how it is possible to setup what values should appear in an array *initially when it is *___//first created by placing them in curly brackets *___myfunction(array,var); *___//Call a subroutine, and pass it the array and a variable *___printf("Element 3 of the array is now: %d \n",array2); *___printf("The variable var is: %d \n",var); *} *myfunction(int array5,int var) *{ *___array2=500; *___//If we modify the array here, it actually modifies the original copy in *___the main *___//program. *___var=99; *___//However this wont affect the original copy of var!!! *} *The End :-) *Andrew Gower